﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SQLDMO;
using Microsoft.SqlServer.Management.Smo;
using System.Collections.Specialized;
using System.IO;

namespace Connect
{
    public partial class Attach : Form
    {
        string ServerName ="";
        string UserName="";
        string PassWord="";
        Connection con = new Connection();
        public Attach()
        {
            InitializeComponent();
            this.ServerName = con.rosv();
            this.UserName = con.rous();
            this.PassWord = con.rops();
        }

       
        public Attach(string svn,string usn, string ps)
        {
            InitializeComponent();
            this.ServerName = svn;
            this.UserName=usn;
            this.PassWord=ps;
        }

        private void Attach_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Escape)
            {
                Close();
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            TapTinData.ShowDialog();
            if (string.IsNullOrEmpty(TapTinData.FileName)==true)
            {
                return;
                btnAttach.Enabled = false;
            }
            txtDuongDanData.Text = TapTinData.FileName;
            txtTenDuLieu.Text = TapTinData.SafeFileName.Replace("_Data.mdf", "").Replace(".mdf", "").Replace("Data.mdf", "").Trim();
        
        }
 

        private void btnAttach_Click(object sender, EventArgs e)
        {
            string filelog = "";
            string filedata = "";
            Server mServer = new Server(".");
            mServer.KillAllProcesses(txtTenDuLieu.Text.Trim());
            if (chkdichuyen.Checked == true)
            {
                File.Move(txtDuongDanData.Text.Trim(),txtDuongDanMoi.Text.Trim() + @"\" + TapTinData.SafeFileName);
                File.Move(txtDuongDanLog.Text.Trim(), txtDuongDanMoi.Text.Trim() + @"\" + TapTinLog.SafeFileName);
                filedata = txtDuongDanMoi.Text.Trim() + @"\" + TapTinData.SafeFileName;
                filelog=txtDuongDanMoi.Text.Trim() + @"\" + TapTinLog.SafeFileName;
            }
            else
            {
                filedata = txtDuongDanData.Text.Trim();
                filelog = txtDuongDanLog.Text.Trim();
            }
            
            mServer.AttachDatabase(txtTenDuLieu.Text.Trim(), new StringCollection { filedata.Trim(), (filelog.Trim()) }, AttachOptions.None);
            trangthai.Value = 100;


              //  SQLDMO.Restore oRestore = new RestoreClass();
              //  SQLServer oSQLServer = new SQLServerClass
              //  {
              //      LoginSecure = false
              //  };
              //  if (string.IsNullOrEmpty(UserName)==true)
              //  {
              //      oSQLServer.LoginSecure = true;
              //      oSQLServer.Connect(ServerName, null, null);
              //  }
              //  else
              //  {
              //      oSQLServer.Connect(ServerName, UserName, PassWord);
              //  }
              //  oSQLServer.AttachDB(txtTenDuLieu.Text.Trim(), TapTinData.FileName);
              ////  oSQLServer.AttachDBWithSingleFile(txtTenDuLieu.Text.Trim(), TapTin.FileName);

        }

        private void txtTenDuLieu_TextChanged(object sender, EventArgs e)
        {
                 if (cnSQL.CheckDatabase(txtTenDuLieu.Text)==true)
	            {
                    txtTenDuLieu.BackColor = System.Drawing.Color.LightPink;
                    btnAttach.Enabled = false;
                    return;
	            }
                 else
                {
                        txtTenDuLieu.BackColor = System.Drawing.SystemColors.Window;
                        btnAttach.Enabled = true;
                        return;
	             }
                 trangthai.Value = 0;
        }

        private void btnTimKiem2_Click(object sender, EventArgs e)
        {
            TapTinLog.ShowDialog();
            if (string.IsNullOrEmpty(TapTinLog.FileName) == true)
            {
                return;
                btnAttach.Enabled = false;
            }
            txtDuongDanLog.Text = TapTinLog.FileName;
        }

        private void chkdichuyen_CheckedChanged(object sender, EventArgs e)
        {
            TimKiem3_Click(sender, e);
            if (chkdichuyen.Checked==true)
            {
                this.Size = new Size(this.Size.Width , this.Size.Height + 35);
                trangthai.Location = new Point(trangthai.Location.X, trangthai.Location.Y + 35);
                txtTenDuLieu.Location = new Point(txtTenDuLieu.Location.X, txtTenDuLieu.Location.Y + 35);
                btnAttach.Location = new Point(btnAttach.Location.X, btnAttach.Location.Y + 35);
                label2.Location = new Point(label2.Location.X, label2.Location.Y + 35);
                label5.Visible = true;
                txtDuongDanMoi.Visible = true;
                TimKiem3.Visible = true;
            }
            else
            {
                this.Size = new Size(this.Size.Width , this.Size.Height - 35);
                trangthai.Location = new Point(trangthai.Location.X, trangthai.Location.Y - 35);
                txtTenDuLieu.Location = new Point(txtTenDuLieu.Location.X, txtTenDuLieu.Location.Y - 35);
                btnAttach.Location = new Point(btnAttach.Location.X, btnAttach.Location.Y - 35);
                label2.Location = new Point(label2.Location.X, label2.Location.Y - 35);
                label5.Visible = false;
                txtDuongDanMoi.Visible = false;
                TimKiem3.Visible = false;
            }

        }

        private void TimKiem3_Click(object sender, EventArgs e)
        {
            LuuTruMoi.ShowDialog();
            if (string.IsNullOrEmpty(LuuTruMoi.SelectedPath)==true)
            {
                return;
            }
            txtDuongDanMoi.Text = LuuTruMoi.SelectedPath;

        }


    }
}
